package com.Iconcat.system.controller;


import com.Iconcat.system.entity.CatCarousel;
import com.Iconcat.system.entity.CatNotise;
import com.Iconcat.system.response.Result;
import com.Iconcat.system.service.CatCarouselService;
import com.Iconcat.system.service.CatNotiseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzq
 * @since 2023-11-27
 */
@RestController
@Api(value = "轮播模块", tags = "轮播接口")
@CrossOrigin
@RequestMapping("/system/carousel")
public class CatCarouselController {
    @Resource
    private CatCarouselService catCarouselService;

    @GetMapping("/all")
    public Result All(@RequestParam(required = true,defaultValue = "1") Integer current,
                      @RequestParam(required = true,defaultValue = "6")Integer size

    ){
        Page<CatCarousel> page=new Page<>(current,size);
        LambdaQueryWrapper<CatCarousel> queryWrapper=new LambdaQueryWrapper<>();
        Page<CatCarousel> userPage=catCarouselService.page(page,queryWrapper);
        return Result.ok().data("Banner",userPage);
    }
    @GetMapping("/getInfo/{id}")
    public Result getSystemSetting(@PathVariable Integer id) {
        CatCarousel banner = catCarouselService.getById(id);
        if (banner != null) {
            return Result.ok().data("banner", banner);
        } else {
            return Result.error().message("banner not found");
        }
    }

    @PostMapping("/add")
    public Result createSystemSetting(@RequestBody CatCarousel catCarousel) {
        boolean success = catCarouselService.save(catCarousel);
        if (success) {
            return Result.ok().message("轮播创建成功");
        } else {
            return Result.error().message("轮播创建失败");
        }
    }

    @PostMapping("/update")
    public Result updateSystemSetting(@RequestBody CatCarousel catCarousel) {

        boolean success = catCarouselService.updateById(catCarousel);
        if (success) {
            return Result.ok().message("轮播修改成功");
        } else {
            return Result.error().message("轮播修改失败");
        }
    }

    @PostMapping("/delete/{id}")
    public Result deleteSystemSetting(@PathVariable Integer id) {
        boolean success = catCarouselService.removeById(id);
        if (success) {
            return Result.ok().message("轮播删除成功");
        } else {
            return Result.error().message("轮播删除失败");
        }
    }

    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            // 获取文件名
            String originalFilename = file.getOriginalFilename();
            System.out.println(originalFilename);
//            String rootPath = System.getProperty("user.dir");
            // 宝塔网站根目录路径
            String rootPath = "/www/wwwroot/iconcat.com/dist/images/";
//            String relativePath = "/src/main/resources/images/";
            // 指定文件存储路径
            String filePath = rootPath  + originalFilename;

            // 将文件保存到指定路径
            file.transferTo(new File(filePath));

            // 返回文件的存储路径或其他信息给前端
            return Result.ok().data("imageUrl", "http://206.237.30.121:6372/images/" + originalFilename);
//            return Result.ok().data("imageUrl", "http://127.0.0.1/images/" + originalFilename);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error().message("文件上传失败");
        }
    }
}

